package com.gvfish.backingbean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import net.sf.dozer.util.mapping.DozerBeanMapper;

import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.google.appengine.api.datastore.Text;
import com.gvfish.core.DozerBeanMapperFactory;
import com.gvfish.entity.Article;
import com.gvfish.service.ArticleManager;

@ManagedBean
@RequestScoped
public class ArticleInput {

	private Long categoryId;
	private String title;
	private String description;
	private String keyWords;
	private String author;
	private String from;
	private Text content;
	

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}
	public String edit(){
		FacesContext ctx = FacesContext.getCurrentInstance();
		Long id = Long.parseLong((String) ctx.getExternalContext().getRequestParameterMap().get("id"));
		System.out.println(id);
		return "/article/input.jsf";
	}


	public String submit() {
		ApplicationContext ctx = FacesContextUtils
				.getWebApplicationContext(FacesContext.getCurrentInstance());
		ArticleManager articleManager = (ArticleManager) ctx
				.getBean("articleManager");
		Article article = new Article();
		DozerBeanMapper mapper = DozerBeanMapperFactory.getMapper();
		article.getDetails().setText(content);
		System.out.println(article);
		mapper.map(this, article);
		articleManager.save(categoryId,article);
		return "success";
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setKeyWords(String keyWords) {
		this.keyWords = keyWords;
	}

	public String getKeyWords() {
		return keyWords;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getAuthor() {
		return author;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getFrom() {
		return from;
	}


	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public Long getCategoryId() {
		return categoryId;
	}

	public void setContent(Text content) {
		this.content = content;
	}

	public Text getContent() {
		return content;
	}

}
